//
//  OMGoodCollectCell.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2017/4/19.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMGoodCollectCell.h"

@interface OMGoodCollectCell ()
@property (nonatomic, strong) UIButton *tagButton;
@end


@implementation OMGoodCollectCell

- (void)configureCellWithObj:(id)obj
{

    if ([obj isKindOfClass:[OMFakeGoodData class]]) {
        [self configureWithFakeData:obj];
    }else if ([obj isKindOfClass:[OMProduct class]]) {
        [self configureWithProduct:obj];
    }
}

- (void)configureWithProduct:(OMProduct*)product
{
    if (product == _product) {
        return;
    }
    _product = product;
    [self.goodIconImageView kt_imageWithURLStr:product.image placeHolder:[UIImage imageNamed:@"homepage-loading-260-260"] withSize:CGSizeMake(self.goodIconImageView.size.width*3, self.goodIconImageView.size.height*3)];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString: product.brand?:@""];
        !title.string.length?:[title appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
        [title appendAttributedString:[[NSAttributedString alloc] initWithString:product.name?:@"" attributes:@{NSFontAttributeName:self.nameLabel.font}]];

        NSString *price = product.price.floatValue == 0 ?@"线上预约":[[NSString stringWithFormat:@"￥%.2f",product.price.floatValue]  stringByReplacingOccurrencesOfString:@".00" withString:@""];

        dispatch_async(dispatch_get_main_queue(), ^{
            _brandLabel.attributedText = title;
            _specLabel.text = product.unit?:@" ";
            _priceLabel.text =price;
        });
    });

    if (_product.promotions.count) {
        self.tagButton.hidden = NO;
        [self.tagButton setTitle:_product.promotions.firstObject.name forState:UIControlStateNormal];
    }else {
        self.tagButton.hidden = YES;
    }

}

- (void)configureWithFakeData:(OMFakeGoodData *)fakeGood
{
    [OMFakeAction asyncLoadLocalImage:fakeGood.image inImageView:_goodIconImageView];
    _brandLabel.text = [fakeGood.image stringByReplacingOccurrencesOfString:@".jpg" withString:@""];
    _specLabel.text = fakeGood.spec;
    _priceLabel.text = [NSString stringWithFormat:@"￥%@",fakeGood.price];
}

- (void)configure
{
    [super configure];
    _goodIconImageView = [UIImageView new];
    _goodIconImageView.image = [UIImage imageNamed:@"homepage-loading-260-260"];
    [self.contentView addSubview:_goodIconImageView];
    [_goodIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(20)));
        make.top.equalTo(@(SM_SIZE(20)));
        make.right.equalTo(@(SM_SIZE(-20)));
        make.width.equalTo(_goodIconImageView.mas_height);
    }];

    _brandLabel = [UILabel new];
    _brandLabel.font = SM_CBFont(29);
    _brandLabel.textColor = SM_blackColor;
    [self.contentView addSubview:_brandLabel];
    [_brandLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_goodIconImageView.mas_left);
        make.top.equalTo(_goodIconImageView.mas_bottom).offset(SM_SIZE(20));
        make.right.equalTo(_goodIconImageView.mas_right);
    }];

    _nameLabel = [UILabel new];
    _nameLabel.textColor = SM_blackColor;
    _nameLabel.font = SM_CFont(29);
//    [self.contentView addSubview:_nameLabel];
//    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(_brandLabel.mas_right).offset(SM_SIZE(5));
//        make.centerY.equalTo(_brandLabel.mas_centerY);
//        make.right.equalTo(_goodIconImageView.mas_right);
//    }];

    _specLabel = [UILabel new];
    _specLabel.font = SM_CFont(26);
    _specLabel.textColor = SM_darkGrayColor;
    [self.contentView addSubview:_specLabel];
    [_specLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_goodIconImageView.mas_left);
        make.right.equalTo(_goodIconImageView.mas_right);
        make.top.equalTo(_brandLabel.mas_bottom).offset(SM_SIZE(5));
    }];

    _priceLabel = [UILabel new];
    _priceLabel.textColor = SM_redColor;
    _priceLabel.font = SM_NFont(25);
    [self.contentView addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_goodIconImageView.mas_left);
        make.top.equalTo(_specLabel.mas_bottom).offset(SM_SIZE(20));
    }];

    _plusButton = [UIButton new];
    [_plusButton setImage:[UIImage imageNamed:@"home_category_plus"] forState:UIControlStateNormal];
    [self.contentView addSubview:_plusButton];
    @weakify(self);
    [[_plusButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        [self didSelectBasketButton:x];
    }];
    [_plusButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_goodIconImageView.mas_right);
        make.bottom.equalTo(_priceLabel.mas_bottom);
    }];

    _tagButton = [UIButton new];
    [_tagButton setTitleColor:SM_whiteColor forState:UIControlStateNormal];
    _tagButton.titleLabel.font = SM_CFont(25);
    _tagButton.contentEdgeInsets = UIEdgeInsetsMake(SM_SIZE(5), SM_SIZE(10), SM_SIZE(5), SM_SIZE(10));
    _tagButton.backgroundColor = SM_dotRedColor;
    [self.contentView addSubview:_tagButton];
    [_tagButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.goodIconImageView.mas_left);
        make.top.equalTo(self.goodIconImageView.mas_top);
    }];
    _tagButton.hidden = YES;

}


- (void)didSelectBasketButton:(UIButton *)button
{
    [OMFMDBCartHelper itemAddition:self.product withButtonAnimation:button targetView:KTSystemStorageSingleton.badgeView];
}




@end
